"use strict";

const Base = require("../base");
const LANG = require("../../language"); // 插件语言库
const LANG_T = antSword["language"]["toastr"]; // 通用通知提示
class JarLoader extends Base {
  precheck() {
    if (this.shelltype == "jsp") {
      return true;
    }
    return false;
  }
  createForm(cell) {
    let self = this;
    var str = [
      {
        type: "settings",
        position: "absolute",
      },
      {
        type: "button",
        name: "SelectFile",
        value: LANG["core"]["jar_loader"]["form"]["SelectFile"],
        width: "75",
        inputWidth: 80,
        inputLeft: 15,
        inputTop: 10,
      },
      {
        type: "input",
        name: "JarFilePath",
        labelWidth: 0,
        labelHeight: 0,
        labelAlign: "left",
        inputWidth: 400,
        labelLeft: 30,
        labelTop: 50,
        inputLeft: 105,
        inputTop: 16,
      },
    ];
    var form = cell.attachForm(str);
    form.attachEvent("onButtonClick", function (name, command) {
      const { dialog } = require("electron").remote;
      dialog.showOpenDialog(
        {
          title: "选择Jar文件",
          //默认路径,默认选择的文件
          // defaultPath: "calc.jar",
          //过滤文件后缀
          filters: [
            {
              name: "Jar",
              extensions: ["jar"],
            },
            { name: "All Files", extensions: ["*"] },
          ],
        },
        (result) => {
          console.log(result);
          self.form.setItemValue("JarFilePath", result[0]);
        }
      );
    });
    this.form = form;
    this.parammode = 2;
  }
  getArgs() {
    let args = {};
    this.payloadtype = "default";
    let JarFilePath = this.form.getItemValue("JarFilePath");
    console.log(JarFilePath);
    let jarClassData = this.file2base64(JarFilePath);
    // console.log(jarClassData);
    args["jarClassData"] = jarClassData;

    return args;
  }
  file2base64(file) {
    const fs = require("fs");
    const path = require("path");
    let filePath = path.resolve(file);
    let data = fs.readFileSync(filePath);
    data = Buffer.from(data).toString("base64");
    return data;
  }
}
module.exports = JarLoader;
